package com.woniu.jifen.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.util.ResponseData;
import com.woniu.jifen.entity.ACoupon;
import com.woniu.jifen.entity.TGoods;
import com.woniu.jifen.service.TGoodsService;
import com.woniu.jifen.mapper.TGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author zyq
* @description 针对表【t_goods】的数据库操作Service实现
* @createDate 2024-05-30 14:40:45
*/
@Service
public class TGoodsServiceImpl extends ServiceImpl<TGoodsMapper, TGoods>
    implements TGoodsService{


    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ResponseData<?> addGoods(TGoods goods) {


        TGoods tGoods = new TGoods();
        tGoods.setName(goods.getName());
        tGoods.setTime(new Date());
        tGoods.setGoods(goods.getGoods());

        save(tGoods);

        redisTemplate.delete("goods");

        return new ResponseData<>().ok();
    }

    @Override
    public List<TGoods> queryGoods() {

        String json = redisTemplate.opsForValue().get("goods");
        if (json != null && !json.isEmpty()) {
            // 如果找到，直接返回
            return JSON.parseArray(json, TGoods.class);
        }

        List<TGoods> list = list();
        json = JSON.toJSONString(list);
        redisTemplate.opsForValue().set("goods", json);

        return list;
    }
}




